package com.six.firstpage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.errorprone.annotations.Var;
import com.six.core.entity.Article;
import com.six.core.entity.BottomArticle;
import com.six.core.entity.BottomArticle;
import com.six.firstpage.mapper.ArticleMapper;
import com.six.firstpage.service.BottomArticleService;
import com.six.firstpage.mapper.BottomArticleMapper;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.mapstruct.IterableMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.smartcardio.ATR;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author ZZH
* @description 针对表【tb_bottom_article】的数据库操作Service实现
* @createDate 2023-02-27 10:21:18
*/
@Service
public class BottomArticleServiceImpl extends ServiceImpl<BottomArticleMapper, BottomArticle>
    implements BottomArticleService{
    @Autowired
    private BottomArticleMapper bottomArticleMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<BottomArticle> getAllBottomArticle() {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<BottomArticle> bottomArticles = bottomArticleMapper.selectList(wrapper);

        //找到父级菜单
        List<BottomArticle> father = bottomArticles.stream().filter(item -> item.getParentId().equals("0")).collect(Collectors.toList());


        //调用方法一二级菜单分离
        List<BottomArticle> first = first(father, bottomArticles);



        return first;
    }

    //查询底部链接
    @Override
    public Page<BottomArticle> getBottomArticleByTitle(String title, Integer status, Integer current, Integer pageSize) {

        Page<BottomArticle> page = new Page<>(current, pageSize);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        if (title!=null&&!title.isEmpty()){
            wrapper.like("name",title);
        }
        if(status!=null){
            wrapper.eq("is_disable",status);
        }

        wrapper.eq("is_deleted",0);

        //查询出所有的底部链接
        List<BottomArticle> bottomArticles = bottomArticleMapper.selectList(wrapper);

        //查询出所有的父级
        wrapper.eq("parent_id",0);
        Page<BottomArticle> fatherPage = bottomArticleMapper.selectPage(page, wrapper);
        List<BottomArticle> father = fatherPage.getRecords();

        //查询出所有的底部文章
        List<Article> articles = articleMapper.selectList(null);

        //文章标题和文章内容对应
        bottomArticles.stream().forEach(
                item->{
                    articles.stream().forEach(
                            article -> {
                                if (item.getId().equals(article.getNavId())&&article.getIsDeleted()!=1){
                                    item.setArticle(article);
                                }
                            }
                    );
                }
        );


        List<BottomArticle> first = first(father,bottomArticles);
        page.setRecords(first);
        return page;
    }

    //添加底部文章
    @Override
    public Integer addBottomArticle(BottomArticle bottomArticle) {
        Integer insert = null;
        if (bottomArticle.getParentId()!=null&&!bottomArticle.getParentId().equals("0")){
            insert  = bottomArticleMapper.insert(bottomArticle);
            Article article = new Article();
            article.setArtTitle(bottomArticle.getName());
            article.setNavId(bottomArticle.getId());
            articleMapper.insert(article);
            return insert;
        }

        insert  = bottomArticleMapper.insert(bottomArticle);

        return insert;

    }

    //修改底部文章
    @Override
    public Integer updateBottomArticle(BottomArticle bottomArticle) {
        return bottomArticleMapper.updateById(bottomArticle);
    }

    //逻辑删除底部文章
    @Override
    public Integer delBottomArticle(String id) {
        UpdateWrapper<BottomArticle> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_deleted",1);


        UpdateWrapper<Article> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("nav_id",id);
        wrapper1.set("is_deleted",1);
        int update = articleMapper.update(null, wrapper1);

        return bottomArticleMapper.update(null, wrapper);
    }


    //底部文章的标题及子标题
    private List<BottomArticle> first(List<BottomArticle> father ,List<BottomArticle> bottomArticles){
//        List<BottomArticle> collect = bottomArticles.stream().filter(item -> item.getParentId().equals("0")).collect(Collectors.toList());
//
//        for (BottomArticle bottomArticle : collect) {
//            List<BottomArticle> second = bottomArticles.stream().filter(item -> item.getParentId().equals(bottomArticle.getId())).collect(Collectors.toList());
//            bottomArticle.setChildren(second);
//        }

        father.stream().forEach(item->{
            List<BottomArticle> collect = bottomArticles.stream().filter(bottomArticle ->
                    bottomArticle.getParentId().equals(item.getId())
            ).collect(Collectors.toList());
            item.setChildren(collect);
        });


        return father;

    }
}




